package com.kingwang.training.designpattern.exam.bo;

import com.kingwang.training.designpattern.exam.bo.event.EventHandler;
import com.kingwang.training.designpattern.exam.bo.event.TaskCreatedEvent;
import com.kingwang.training.designpattern.exam.dao.TaskDao;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 工单通知生成器
 *
 * @author King
 */
@Component
public class TaskNoticeGenerator implements EventHandler<TaskCreatedEvent> {
    @Resource
    private TaskDao taskDao;

    public void generateNotice(DepartmentContact contact) {
        Notice notice = new Notice();

        notice.setTitle("维修工单通知");
        notice.setContent("有新的维修工单！");
        notice.setReceiverId(contact.getUserId());
        notice.setCreateDateTime(LocalDateTime.now());

        this.taskDao.addNotice(notice);

        System.out.println("已生成通知：" + notice.toString());
    }

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public void handle(TaskCreatedEvent event) {
        System.out.println("接收到TaskCreatedEvent：" + event.toString());

        DepartmentContact contact = this.taskDao.getProductDepartmentContact();

        generateNotice(contact);
    }
}
